package com.ggboy.item_bank.controller;

import com.ggboy.item_bank.beans.*;
import com.ggboy.item_bank.service.PaperService;
import com.ggboy.item_bank.service.SubjectService;
import com.ggboy.item_bank.service.TestService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/paper")
public class PaperController {
    @Resource
    private TestService testService;

    @Resource
    private PaperService paperService;

    @Resource
    private SubjectService subjectService;

    @RequestMapping("/capacityPaper")//取题用的
    public String capacityPaper(String subject, Integer choiceCount, Integer tofCount,
                                Integer designCount, Integer bigCount, Integer level, HttpSession session, Model model){
        try {
            User user = (User) session.getAttribute("user");//取出登录账号的全部数据
//            Paper paper = paperService.capacity(subject,choiceCount,tofCount,designCount,bigCount,level,user.getId());
            paper1 paper = paperService.capacity1(subject,choiceCount,tofCount,designCount,bigCount,level,user.getId());

            session.setAttribute("paper",paper);//用会话保存取出的题目的信息


            //根据每种类型提的ID去对应的表获取每种类型题的javaBean对象并把他们存进集合中
            List<ChoiceQst> choiceQsts = testService.selectChoiceListById1(paper.getPaperChoiceInfo());
            List<TOFQst> tofQsts = testService.selectTOFListById1(paper.getPaperTOFInfo());
            List<DesignQst> designQsts = testService.selectDesignListById1(paper.getPaperDesignInfo());
            List<BigQst> bigQsts = testService.selectBigListById1(paper.getPaperBigInfo());
            session.setAttribute("xz",choiceQsts);//改题要用
            //把每种javaBean对象集合用model保存
            model.addAttribute("choiceQsts",choiceQsts);
            model.addAttribute("tofQsts",tofQsts);
            model.addAttribute("designQsts",designQsts);
            model.addAttribute("bigQsts",bigQsts);
            return "capacityPaper1";
        }catch (Exception e){
            User user = (User) session.getAttribute("user");//取出登录账号的全部数据
            model.addAttribute("msg","试题数量不足，请修改试题数量");
            String teacherSpecialty = user.getTeacherSpecialty();//取出你的学院信息
            int i = Integer.parseInt(teacherSpecialty);//字符串转为int
            List<Subject> subjectList = subjectService.selectSubBySpecialtyId(i);//查询出这个学院的所有课程
            model.addAttribute("subjectList", subjectList);//用model存起来
            return "capacityPaper";
        }

    }

    @GetMapping("/VolumeChange")
    public String xzVolumeChange(@RequestParam Map<String,Object> map, Model model, HttpSession session){//选择题批改
        List<ChoiceQst> choiceQsts = (List<ChoiceQst>) session.getAttribute("xz");//取出这次提交的题目选择题的所有信息
        List<ChoiceQst> choiceQsts1 = new ArrayList<>();
        for (ChoiceQst C: choiceQsts) {//遍历选择题
            if(map.get(String.valueOf(C.getChoiceQstId())).equals(C.getChoiceQstAnsw())){//根据传回来的name获取到对应的键然后再和对应的javabean的答案属性做比较
                C.setPd(1);//正确标记一个1
                C.setMy((String) map.get(String.valueOf(C.getChoiceQstId())));
                choiceQsts1.add(C);
            }else {//错误标记一个2
                C.setPd(2);
                C.setMy((String) map.get(String.valueOf(C.getChoiceQstId())));
                choiceQsts1.add(C);
            }
        }
        model.addAttribute("choiceQsts",choiceQsts1);//用model保存起来
        return "capacityPaper2";
    }
}
